package com.yangtu.nearbyshop.admin.web;

import com.yangtu.nearbyshop.admin.annotation.RequiresPermissionsDesc;
import com.yangtu.nearbyshop.admin.dao.MercInf;
import com.yangtu.nearbyshop.admin.service.AdminMercService;
import com.yangtu.nearbyshop.admin.service.AdminOrderService;
import com.yangtu.nearbyshop.core.validator.Order;
import com.yangtu.nearbyshop.core.validator.Sort;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

@RestController
@RequestMapping("/admin/merc")
@Validated
public class AdminMercController {
    private final Log logger = LogFactory.getLog(AdminMercController.class);

    @Autowired
    private AdminMercService mercService;

    @Autowired
    private AdminOrderService orderService;

    @RequiresPermissions("admin:merc:list")
    @RequiresPermissionsDesc(menu={"团长管理" , "团长列表"}, button="查询")
    @GetMapping("/list")
    public Object list(String mercNm, String mobile,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        return mercService.list(mercNm, mobile, page, limit, sort, order);
    }

    @RequiresPermissions("admin:merc:detail")
    @RequiresPermissionsDesc(menu={"团长管理" , "团长列表"}, button="详情")
    @GetMapping("/detail")
    public Object detail(@NotNull Integer id) {
        return mercService.detail(id);
    }

    @RequiresPermissions("admin:merc:delete")
    @RequiresPermissionsDesc(menu={"团长管理" , "团长列表"}, button="删除")
    @GetMapping("/delete")
    public Object delete(@NotNull Integer id) {
        return mercService.delete(id);
    }

    @RequiresPermissions("admin:merc:update")
    @RequiresPermissionsDesc(menu={"团长管理" , "团长列表"}, button="编辑")
    @PostMapping("/update")
    public Object update(@RequestBody MercInf mercInf) {
        return mercService.update(mercInf);
    }

    @RequiresPermissions("admin:merc:create")
    @RequiresPermissionsDesc(menu={"团长管理" , "新增团长"}, button="添加")
    @PostMapping("/create")
    public Object add(@RequestBody MercInf mercInf) {
        return mercService.create(mercInf);
    }

    @RequiresPermissions("admin:merc:profit")
    @RequiresPermissionsDesc(menu={"团长管理" , "提成明细"}, button="查询")
    @GetMapping("/profit")
    public Object profit(String mercNo, String startDt, String endDt,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit) {
        return orderService.getProfitList(mercNo, startDt, endDt, page, limit);
    }
}
